package com.dhq.myapp.util.http

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import java.io.IOException

/**
 * DESC
 * Created by admin on 2017/11/16.
 */
class StringNullAdapter : TypeAdapter<String>() {

    @Throws(IOException::class)
    override fun write(writer: JsonWriter, value: String?) {
        if (value == null) {
            writer.nullValue()
            return
        }
        writer.value(value)
    }

    @Throws(IOException::class)
    override fun read(reader: JsonReader): String {
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull()
            return ""
        }

        val jsonStr = reader.nextString()

        // 一般不过滤 防止用户刻意将文字写成字符串"null"
        // if (jsonStr == "null") {
        //     return ""
        // }

        return jsonStr
    }
}
